﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using DotSpatial.Data;

using DotSpatial.Controls;

using DotSpatial.Topology;

using DotSpatial.Symbology;



namespace HAGISV2
{

    /// <summary>
    /// 为混合图形图层提供基础,即点，线，面同时提供的图层
    /// </summary>
    [Serializable]
   public class HybridBaseLayer:Layer
    {
        public HybridBaseLayer(string id):base(id)
        {
            this._isSelectable = true;
            this.BuildDataset();

        }



        #region 受保护的字段

        [NonSerialized]
        protected FeatureSet _ptFeatureSet;
        [NonSerialized]
        protected FeatureSet _lnFeatureSet;
        [NonSerialized]
        protected FeatureSet _pyFeatureSet;


        [NonSerialized]
        protected MapLineLayer _lnLayer;

        public MapLineLayer LnLayer
        {
            get { return _lnLayer; }

        }


        [NonSerialized]
        protected MapPointLayer _ptLayer;

        public MapPointLayer PtLayer
        {
            get { return _ptLayer; }

        }

        [NonSerialized]
        protected MapPolygonLayer _pyLayer;

        public MapPolygonLayer PyLayer
        {
            get { return _pyLayer; }

        }


   




        #endregion


        #region 公共属性

        public override Extent GetLayerExtent()
        {
            if (!this.IsLoaded||this._viewMode!= FMAPVIEW.NORMAL)
            {
                return null;
            }

            Extent ex = new Extent();
            if (_ptLayer.DataSet.Features.Count > 0)
            {
                _ptLayer.DataSet.UpdateExtent();
                ex.ExpandToInclude(_ptLayer.Extent);
            }
            if (_lnLayer.DataSet.Features.Count > 0)
            {
                _lnLayer.DataSet.UpdateExtent();
                ex.ExpandToInclude(_lnLayer.Extent);
            }
            if (_pyLayer.DataSet.Features.Count > 0)
            {
                _pyLayer.DataSet.UpdateExtent();
                ex.ExpandToInclude(_pyLayer.Extent);
            }

            if (ex == new Extent())
                return null;

            return ex;
        }


        /// <summary>
        /// 该图层包含的gis图层数量
        /// </summary>
        public override int MapLayerCount
        {
            get
            {
                return 3;
            }
        }
       


        public string LayerFileNamePt
        {
            get { return this.DefaultManager.layerPath + this.Id + "_pt.shp"; }
        }

        public string LayerFileNameLn
        {
            get { return this.DefaultManager.layerPath + this.Id + "_ln.shp"; }
        }

        public string LayerFileNamePy
        {
            get { return this.DefaultManager.layerPath + this.Id + "_py.shp"; }
        }


        #endregion


        #region 受保护的方法


        protected virtual void BuildDataset()
        {
            _ptFeatureSet = new FeatureSet(FeatureType.Point);

            _pyFeatureSet = new FeatureSet(FeatureType.Polygon);

            _lnFeatureSet = new FeatureSet(FeatureType.Line);

         
       

        }

        protected virtual void LoadFeatures()
        {

            //this._ptFeatureSet.Open(this.LayerFileNamePt);
            //this._lnFeatureSet.Open(this.LayerFileNameLn);

            //this._pyFeatureSet.Open(this.LayerFileNamePy);


            this._ptFeatureSet.InvalidateVertices();
            this._ptFeatureSet.UpdateExtent();

            this._lnFeatureSet.InvalidateVertices();
            this._lnFeatureSet.UpdateExtent();


            this._pyFeatureSet.InvalidateVertices();
            this._pyFeatureSet.UpdateExtent();



           





        }

        protected virtual void SaveFeatures()
        {

            //this._ptFeatureSet.SaveAs(this.LayerFileNamePt, true);
            //this._lnFeatureSet.SaveAs(this.LayerFileNameLn, true);
            //this._pyFeatureSet.SaveAs(this.LayerFileNamePy, true);

        }


    


        #endregion


     

      

       #region 重写的方法

        public override void Render()
        {
            Map map = this.DefaultManager.CurrentMap;

            _pyLayer = (MapPolygonLayer)map.Layers.Add(this._pyFeatureSet);
            _lnLayer = (MapLineLayer)map.Layers.Add(this._lnFeatureSet);
            _ptLayer = (MapPointLayer)map.Layers.Add(this._ptFeatureSet);


            if (this._mapLayerIndex != -1)
            {
                _pyLayer.LockDispose();
                _lnLayer.LockDispose();
                _ptLayer.LockDispose();

                map.Layers.Remove(_pyLayer);
                map.Layers.Remove(_lnLayer);
                map.Layers.Remove(_ptLayer);

                map.Layers.Insert(this._mapLayerIndex, _ptLayer);
                map.Layers.Insert(this._mapLayerIndex, _lnLayer);
                map.Layers.Insert(this._mapLayerIndex, _pyLayer);

                _pyLayer.UnlockDispose();
                _lnLayer.UnlockDispose();
                _ptLayer.UnlockDispose();

                this._mapLayerIndex = -1;
            }

            this._isLoaded = true;

        }



       public override void StopRender()
       {

           Map map = this.DefaultManager.CurrentMap;

           map.Layers.Remove(this._ptLayer);
           map.Layers.Remove(this._lnLayer);
           map.Layers.Remove(this._pyLayer);

       



           this._isLoaded = false;

          
       }


       public override bool OpenLayer()
       {
           if (this._ptLayer == null)
           {
               this.BuildDataset();

           }
           else
           {

               

           }
          

           this.Render();

           this.LoadFeatures();

           this.OnSetLayerOpacity();
           


           return base.OpenLayer();
       }

       public override bool SaveLayer()
       {

           this.SaveFeatures();

           return base.SaveLayer();

       }


       protected override void OnSetVisible()
       {
           if (!this.IsLoaded)
           {
               return;
               
           }

           this._ptLayer.IsVisible = this.Visible;
           this._lnLayer.IsVisible = this.Visible;
           this._pyLayer.IsVisible = this.Visible;
          
       }




       #region 选择部分

       public override void Select(IEnvelope tolerant, IEnvelope strict, SelectionMode selectionMode)
       {

           IEnvelope env;

           this._ptLayer.Select(tolerant, strict, selectionMode,out env);
           this._lnLayer.Select(tolerant, strict, selectionMode, out env);

           this._pyLayer.Select(tolerant, strict, selectionMode, out env);


           //List<IFeature> selectedFeatures = new List<IFeature>();
           
           //selectedFeatures.AddRange(this._ptLayer.Selection.ToFeatureList());
           //selectedFeatures.AddRange(this._lnLayer.Selection.ToFeatureList());
           //selectedFeatures.AddRange(this._pyLayer.Selection.ToFeatureList());


          
       }


       public override void ClearSelection()
       {
           if (_ptLayer.Selection.Count > 0)
               this._ptLayer.Selection.Clear();
           if (_lnLayer.Selection.Count > 0)
               this._lnLayer.Selection.Clear();
           if (_pyLayer.Selection.Count > 0)
               this._pyLayer.Selection.Clear();
       }




       #endregion


        #endregion







        #region 关于序列化

       


        #endregion


    }
}
